const db = require("../../db");

/**
 * POST 单位的回调函数
 * @param warehouseName  单位名称
 * @param warehousecode  单位代码
 * @param warehouseType  类型
 * @param remark  备注
 */
exports.add = (req, res) => {
  // 接收表单的数据
  const warehouseInfo = req.body;
  let sql = "select * from selection_warehouse where warehouseName=?";
  db.query(sql, [warehouseInfo.warehouseName], (error, result) => {
    if (error) {
      return res.cc(error);
    }
    if (result.length > 0) {
      return res.cc("仓库名称已被占用!");
    }
    // 定义 SQL 语句
    let sql1 = "insert into selection_warehouse set ?";
    db.query(
      sql1,
      {
        warehouseName: warehouseInfo.warehouseName,
        createdTime: Date.now(),
        remark: warehouseInfo.remark,
      },
      (error, result) => {
        if (error) return res.cc(error);
        // 判断影响行数是否为 1
        if (result.affectedRows !== 1) return res.cc("新增仓库失败！");
        return res.cc("新增仓库成功", true);
      }
    );
  });
};

exports.del = (req, res) => {
  // 接收表单的数据
  const warehouseInfo = req.body;
  let sql = "select * from selection_warehouse where id=?";
  db.query(sql, [warehouseInfo.id], (error, result) => {
    if (error) {
      return res.cc(error);
    }
    if (result.length === 0) {
      return res.cc("未找到该仓库!");
    }
    // 定义 SQL 语句
    const sql1 = "delete from selection_warehouse where id=?";
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, warehouseInfo.id, (error, result) => {
      if (error) return res.cc(error);
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc("删除仓库失败！");
      return res.cc("删除仓库成功", true);
    });
  });
};

exports.update = (req, res) => {
  // 接收表单的数据
  const warehouseInfo = req.body;
  let sql = "select * from selection_warehouse where id=?";
  db.query(sql, [warehouseInfo.id], (error, result) => {
    if (error) {
      return res.cc(error);
    }
    if (result.length === 0) {
      return res.cc("未找到该仓库!");
    }
    // 更新仓库信息
    const updateSQL =
      "UPDATE selection_warehouse SET warehouseName=?, remark=? WHERE id=?";
    const updateValues = [
      warehouseInfo.warehouseName,
      warehouseInfo.remark,
      warehouseInfo.id,
    ];

    db.query(updateSQL, updateValues, (error, result) => {
      if (error) {
        return res.cc(error);
      }

      if (result.affectedRows !== 1) {
        return res.cc("修改仓库失败！");
      }

      return res.cc("修改仓库成功", true);
    });
  });
};

exports.list = (req, res) => {
  const page = req.body.page ? parseInt(req.body.page) : 1;
  const limit = req.body.limit ? parseInt(req.body.limit) : 10;
  const warehouseName = req.body.warehouseName || "";
  const sqlCount = "SELECT COUNT(*) AS total FROM selection_warehouse";

  db.query(sqlCount, (error, countResults) => {
    if (error) {
      return res.cc(error);
    }

    const total = countResults[0].total;
    const offset = (page - 1) * limit;
    let sql;
    let params;

    if (warehouseName) {
      sql = `SELECT * FROM selection_warehouse WHERE warehouseName LIKE ?  LIMIT ? OFFSET ?`;
      params = [`%${warehouseName}%`, limit, offset];
    }

    db.query(sql, params, (error, results) => {
      if (error) {
        return res.cc(error);
      }

      return res.cc("获取记录成功", true, results, {
        total,
        page,
        limit,
      });
    });
  });
};
